﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using syc.Helper;
using System.Drawing;

namespace syc.ZhiJian.Report
{
    public partial class 发货供货明细 : System.Web.UI.Page
    {
        protected string year;
        protected string month;
        protected string RiQiStart;
        protected string RiQiEnd;
        protected string RiQi;
        protected DateTime start, end;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";
            
            
            if (!this.IsPostBack)
            {
                //调用通用处理逻辑段【各个下拉列表变化时都调用了它】
                DanWei.DataBind();
            }

            //这一段分组的代码逻辑会强制执行，所以无需再在各个事件中重写
            GridViewHelper helper = new GridViewHelper(this.GridView1);
            helper.RegisterGroup("出库日期", true, true);
            helper.RegisterSummary("捆数", SummaryOperation.Sum, "出库日期");
            helper.RegisterSummary("支数", SummaryOperation.Sum, "出库日期");
            helper.RegisterSummary("重量", SummaryOperation.Sum, "出库日期");
            helper.RegisterSummary("长度", SummaryOperation.Sum, "出库日期");
            helper.RegisterSummary("车数", SummaryOperation.Sum, "出库日期");
            helper.RegisterSummary("捆数", SummaryOperation.Sum);
            helper.RegisterSummary("支数", SummaryOperation.Sum);
            helper.RegisterSummary("重量", SummaryOperation.Sum);
            helper.RegisterSummary("长度", SummaryOperation.Sum);
            helper.RegisterSummary("车数", SummaryOperation.Sum);

            helper.GroupHeader += new GroupEvent(helper_GroupHeader);
            helper.GroupSummary += new GroupEvent(helper_GroupSummary);
            helper.GeneralSummary += new FooterEvent(helper_GeneralSummary);
            helper.ApplyGroupSort();
        }

        //自定义分组标题样式
        private void helper_GroupHeader(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            row.Cells[0].Height = 33;
            row.BackColor = Color.LightGray;
            row.Cells[0].Text = "&nbsp;&nbsp;" + row.Cells[0].Text;

        }
        //自定义合计标题样式
        private void helper_GeneralSummary(GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            row.BackColor = Color.LightSlateGray;
            row.Cells[0].Text = "总计:";
            row.Height = 40;
        }
        //自定义小计标题样式
        private void helper_GroupSummary(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            row.BackColor = Color.LightSalmon;
            row.Cells[0].Text = "小计:";
            row.Height = 40;
        }

        //导出按钮
        protected void Export_Click(object sender, EventArgs e)
        {
            //调用通用处理逻辑段【各个下拉列表变化时都调用了它】
            GridView1.DataBind();
            GridViewHelper helper = new GridViewHelper(this.GridView1);
            helper.RegisterGroup("出库日期", true, true);
            helper.RegisterSummary("捆数", SummaryOperation.Sum, "出库日期");
            helper.RegisterSummary("支数", SummaryOperation.Sum, "出库日期");
            helper.RegisterSummary("重量", SummaryOperation.Sum, "出库日期");
            helper.RegisterSummary("长度", SummaryOperation.Sum, "出库日期");
            helper.RegisterSummary("捆数", SummaryOperation.Sum);
            helper.RegisterSummary("支数", SummaryOperation.Sum);
            helper.RegisterSummary("重量", SummaryOperation.Sum);
            helper.RegisterSummary("长度", SummaryOperation.Sum);
            helper.RegisterSummary("车数", SummaryOperation.Sum);
            helper.GroupHeader += new GroupEvent(helper_GroupHeader);
            helper.GroupSummary += new GroupEvent(helper_GroupSummary);
            helper.GeneralSummary += new FooterEvent(helper_GeneralSummary);
            helper.ApplyGroupSort();
            //var ss = GridViewExportHelper.GetExportGridContent(GridView1, false,false);
            //GridViewExportHelper.WriteResponse(ss, "application/vnd.ms-excel", true, "Filename.xls");
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.Charset = "utf-8";
            // If you want the option to open the Excel file without saving than
            // comment out the line below
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.xls";
            //设置EXCEL的表头信息
            var title = Date1.Text + "到" + Date2.Text + DanWei.SelectedItem.Text + DList_Produce.SelectedItem.Text + "发货供货明细";
            HttpContext.Current.Response.Write(
    @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html>
<head>
<meta http-equiv=""Content-Language"" content=""zh-cn""/>
<meta http-equiv=""Content-Type"" content="""
    + HttpContext.Current.Response.ContentType + "; charset=utf-8\"/></head><body><h2><center>" + title + "</center></h2>");
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GridView1.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            HttpContext.Current.Response.Write("</body></html>");
            Response.End();

        }

        //为了导出而重载的一个事件，具体看EXPORT TO EXCEL的文章
        public override void VerifyRenderingInServerForm(Control control)
        {

            // Confirms that an HtmlForm control is rendered for the
            //specified ASP.NET server control at run time.

        }

    }
}
